import traceback

from sqlalchemy import Column, String, Text, DateTime, JSON
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm.session import Session

from eshoputils.mysql_util import DataBase

Base = declarative_base()

class SpiderUser(Base):
    # 定义表名,和数据库一致
    __tablename__ = 'spider_user'
    # 定义字段
    id = Column(String(32), primary_key=True)
    user_name = Column(String(1024))
    user_pass = Column(String(100))
    role_id = Column(String(32))

    @classmethod
    def insert_user(cls, **kwargs):
        """
        插入task
        """
        session = Session(bind=DataBase.get_engin())
        try:
            images_injuries_task_entity = SpiderUser(**kwargs)
            session.add(images_injuries_task_entity)
            session.commit()
        except Exception as e:
            traceback.print_exc()
            session.rollback()
        finally:
            session.close()


    @classmethod
    def select_user_by_id(cls,user_id):
        """
        查询结果
        """
        session = Session(bind=DataBase.get_engin())
        try:
            res = session.query(SpiderUser).filter_by(id=user_id)
            return  res
        finally:
            session.close()

    @classmethod
    def select_user_by_name(cls,user_name):
        """
        查询结果
        """
        session = Session(bind=DataBase.get_engin())
        try:
            res = session.query(SpiderUser).filter_by(user_name=user_name).first()
            return  res
        finally:
            session.close()


